package cn.com.dev.temp;

import cn.com.kh.FDDUtils;
import cn.com.kh.HttpClientProxy;
import cn.com.kh.KHUtil;
import cn.com.kh.TxConfig;
import cn.com.kh.batch.FCallAPI_Utils;
import cn.com.kh.result.iAPIResult;
import net.sf.json.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

/**
 * @projectName: ngsapitask-ph
 * @package: cn.com.dev
 * @className: FDD_SDK
 * @author: LiJT
 * @description: TODO
 * @date: 2024-06-27 18:25
 * @version: 1.0
 */
public class FDD_FixAuthTask {
    private static final Log log = LogFactory.getLog(FDD_FixAuthTask.class);
    private static String CC_APIDomain = "https://ronglian.dbc61.com/ngsapitask_ph/api";
    private static String APPID = "100002";
    private static String APPKEY = "E4uX9c65e5h7A5K5DA6rw6JL";

    // 获取数据库操作对象
    private static JdbcTemplate jt = new JdbcTemplate();

    public static void main(String[] args) throws Exception {
        jt = new JdbcTemplate(TxConfig.getDruidDataSource());

        // 获取待认证客户状态
        batchFDDCustFix();

    }


    // Fix-1-获取待认证客户客户
    public static void batchFDDCustFix() throws InterruptedException {
        String dealSQL = "Select R1.Oper_Id, R1.Rec_Guid, R1.Customer_Id, R1.Customer_Guid, R1.Fdd_Customer_Id, R1.Push_Type, R1.Status, R1.Statusdesc, R2.Customername, R2.Mobile, R2.Customertype, R2.Customerid From Sys_Oper_Log_Auth R1 Left Join Customer R2 On R1.Customer_Guid = R2.Customer_Guid Where (R1.Status != If(Ifnull(R2.Customertype, '个人') = '公司', '4', '2') Or R1.Status Is Null Or R1.Status = '') And R1.Push_Time > Date_Add(Now(), Interval -30 Day) Order By R1.Push_Time Desc";
        List<Map<String, Object>> selectCus = jt.queryForList(dealSQL);
        log.debug("Fix-法大大客户基础信息 :" + selectCus.size());

        // 处理节点数据
        for (Map<String, Object> CusMap : selectCus) {
            String Oper_Id = KHUtil.GetMapV(CusMap, "Oper_Id");
            String Rec_Guid = KHUtil.GetMapV(CusMap, "Rec_Guid");
            String Fdd_Customer_Id = KHUtil.GetMapV(CusMap, "Fdd_Customer_Id");
            String Customer_Guid = KHUtil.GetMapV(CusMap, "Customer_Guid");
            String Customername = KHUtil.GetMapV(CusMap, "Customername");
            String Customerid = KHUtil.GetMapV(CusMap, "Customerid");
            String Mobile = KHUtil.GetMapV(CusMap, "Mobile");
            String Customertype = KHUtil.GetMapV(CusMap, "Customertype");
            Customertype = KHUtil.isNull(Customertype) ? "个人" : Customertype;
            try {
                // 检查当前客户最新认证状态
                if (Customertype.equals("个人")) {
                    JSONObject get_Json = FCallAPI_Utils.getFDDPersonInfo(jt, Rec_Guid, Mobile);
                    log.error("ℹ获取[个人认证][" + Rec_Guid + "]法大大客户基础信息 结果：" + get_Json);

                    if (KHUtil.GetSFJV(get_Json, "code").equals("1") && KHUtil.GetSFJV(get_Json.getJSONObject("data"), "code").equals("1") && KHUtil.GetSFJV(get_Json.getJSONObject("data").getJSONArray("data").getJSONObject(0), "verifiedStatus").equals("2")) {
                        get_Json = SendCert_Callback(Rec_Guid, Customerid, "审核通过", "个人认证");
                        log.error("✅获取[个人认证][" + Rec_Guid + "]模拟回调 结果：" + get_Json);
                    }
                } else {
                    JSONObject get_Json = FCallAPI_Utils.getFDDCompanyInfo(jt, Rec_Guid, Customername);
                    log.error("ℹ获取[企业认证][" + Rec_Guid + "]法大大客户基础信息 结果：" + get_Json);

                }
            } catch (Exception e) {
                log.error("⛔检查当前客户最新认证状态 异常", e);

            }

            Thread.sleep(200);

        }

    }

    // Fix-2-模拟认证回调
    public static JSONObject SendCert_Callback(String recGuid, String customerId, String statusDesc, String authType) {

        JSONObject res_Json = iAPIResult.ERROR("推送-模拟认证回调 失败");
        try {

            JSONObject jsonPara = new JSONObject();
            jsonPara.put("customerId", customerId);
            jsonPara.put("statusDesc", statusDesc);

            if (statusDesc.equals("审核通过")) {
                jsonPara.put("status", "2");
                jsonPara.put("auditFailReason", "");
            } else {
                jsonPara.put("status", "1");
                jsonPara.put("auditFailReason", "模拟回调认证失败");

            }

            JSONObject jsons = FDDUtils.getCallBody(APPID, APPKEY, jsonPara.toString());
            log.debug("模拟认证回调 参数:" + jsons.toString());

            String callResStr = HttpClientProxy.sendPost(CC_APIDomain + "/apiBatchSignMain?callbackType=" + KHUtil.encode(authType) + "&customerId=" + customerId + "&recGuid=" + KHUtil.encode(recGuid), jsons.toString(), "", 0);
            res_Json = JSONObject.fromObject(callResStr);
            log.debug("模拟认证回调 结果:" + callResStr);

        } catch (Exception e) {
            log.error("模拟认证回调 异常", e);

        }
        return res_Json;

    }
}